package com.inool.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.inool.common.login.LoginUser;
import com.inool.common.login.LoginUserHolder;
import com.inool.common.result.Result;
import com.inool.model.Grade;
import com.inool.model.dto.GradeDto;
import com.inool.service.GradeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "成绩管理")
@RestController
@RequestMapping("/api/grades")
public class GradeController {

    @Autowired
    private GradeService gradeService;

    @Operation(summary = "添加或修改成绩")
    @PostMapping("add")
    public Result<Boolean> addGrade(@RequestBody Grade grade) {
        return Result.ok(gradeService.saveOrUpdateGrade(grade));
    }

    @Operation(summary = "修改成绩信息")
    @PostMapping("/update/{id}")
    public Result<Grade> updateGrade(@PathVariable Long id, @RequestBody Grade grade) {
        Grade updatedGrade = gradeService.updateGrade(id, grade);
        if (updatedGrade != null) {
            return Result.ok(updatedGrade);
        }
        return Result.ok();
    }

    @Operation(summary = "删除成绩")
    @PostMapping("/delete/{id}")
    public Result<Void> deleteGrade(@PathVariable Integer id) {
        gradeService.deleteGrade(id);
        return Result.ok();
    }

    @Operation(summary = "获取成绩信息")
    @GetMapping("get/{id}")
    public Result<Grade> getGradeById(@PathVariable Integer id) {
        Grade grade = gradeService.getGradeById(id);
        if (grade != null) {
            return Result.ok(grade);
        }
        return Result.ok();
    }

    @Operation(summary = "获取所有成绩信息")
    @GetMapping("/all")
    public Result<List<Grade>> getAllGrades() {
        List<Grade> grades = gradeService.getAllGrades();
        return Result.ok(grades);
    }

    @Operation(summary = "根据学生 ID 获取成绩信息")
    @GetMapping("/student/{studentId}")
    public Result<List<Grade>> getGradesByStudentId(@PathVariable Integer studentId) {
        List<Grade> grades = gradeService.getGradesByStudentId(studentId);
        return Result.ok(grades);
    }

    @Operation(summary = "根据课程 ID 获取成绩信息")
    @GetMapping("/course/{courseId}")
    public Result<List<Grade>> getGradesByCourseId(@PathVariable Integer courseId) {
        List<Grade> grades = gradeService.getGradesByCourseId(courseId);
        return Result.ok(grades);
    }

    @Operation(summary = "分页查询课程，可根据课程名和学号查询")
    @GetMapping("/page")
    public Result<Page<GradeDto>> pageGrade(@RequestParam long current, @RequestParam long size,
                                            String studentName, String courseName) {
        Page<GradeDto> page = gradeService.pageGrade(current, size, studentName, courseName);
        return Result.ok(page);
    }

    @Operation(summary = "学生分页查询本人成绩")
    @GetMapping("/page/student")
    public Result<Page<GradeDto>> pageStudentGrade(@RequestParam long current, @RequestParam long size) {
        LoginUser loginUser = LoginUserHolder.getLoginUser();

        Page<GradeDto> page = gradeService.pageStudentGrade(current, size, loginUser.getUserId());
        return Result.ok(page);
    }
}    